import modelExtend from 'dva-model-extend'
import { query, store, update, publish, audit, cancel } from 'services/financialAccountBalanceChangeApply'
import { listPaymenters } from 'services/financialReceipt'
import { detail as distributorDetail } from 'services/distributor'
import { lwjPageModel } from 'utils/model'
import { routerRedux } from 'dva/router'
import { config } from 'utils'
import { message } from 'antd'

const { defaultPageSize } = config
export default modelExtend(lwjPageModel, {
  namespace: 'balanceChangeApply',
  state: {
    currentItem: {},
    formModalVisible: false,
    auditModalVisible: false,
    viewModalVisible: false,
    submitModalVisible: false,
    selectedTags: [],
    isExpand: false,
    distributorState: {
      items: [],
      pagination: {
        current: 1,
        total: 0,
        showTotal: total => `共 ${total} 条`,
        showSizeChanger: true,
        showQuickJumper: true,
        defaultPageSize: defaultPageSize
      },
      pageOptions: {
        sort: 'createTime',
        order: 'DESC'
      },
      queryParam: {},
      distributorModalVisible: false,
      currentItem: {},
      contractNumVisible: false
    },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(({ pathname }) => {
        /**
         * 列表页的监听
         */
        if (pathname === '/financial/balanceChangeApply') {
          dispatch({ type: 'query', payload: {} })
        }
      })
    },
  },
  effects: {
    * query({ payload }, { call, put, select }) {
      yield put({
        type: 'asyncData',
        payload: {
          ...payload
        }
      })
      const pageOptions = yield select(({ balanceChangeApply }) => balanceChangeApply.pageOptions)
      const queryParam = yield select(({ balanceChangeApply }) => balanceChangeApply.queryParam)

      const queryPayload = {
        ...pageOptions,
        queryParam: queryParam
      }

      const data = yield call(query, queryPayload)

      yield put({
        type: 'querySuccess',
        payload: {
          ...data
        }
      })
    },

    /**
     * 根据状态标签查询
     * @param {*} param0
     * @param {*} param1
     */ * queryBySelectedTags({
      payload
    }, { select, call, put }) {
      yield put({
        type: 'changeSelectedTags',
        payload: {
          selectedTags: payload.selectedTags
        }
      })
      yield put({
        type: 'query',
        payload: {
          'status': payload.selectedTags
        }
      })
    },

    * store({ payload }, { call, put, select }) {
      const res = yield call(store, payload)

      if (res.success) {
        yield put({
          type: 'hideModal',
          payload: {}
        })
      } else {
        message.error(res.message)
      }
    },

    * audit({ payload }, { call, put, select }) {
      const res = yield call(audit, payload)
      if (res.success) {
        yield put({
          type: 'hideModal',
          payload: {}
        })
        yield put({
          type: 'query',
          payload: {}
        })
      }
    },

    * update({ payload }, { call, put, select }) {
      const res = yield call(update, payload)
      if (res.success) {
        yield put({
          type: 'hideModal',
          payload: {}
        })
        message.success('编辑成功')
      }
    },

    * publish({ payload }, { call, put, select }) {
      const { id, version } = payload
      const res = yield call(publish, { id, version })

      if (res.success) {
        message.success('提交成功')
        yield put({
          type: 'query',
          payload: {}
        })
        yield put({
          type: 'hideModal',
          payload: {}
        })
      }
    },

    * cancel({ payload }, { call, put, select }) {
      const { id, version } = payload
      const res = yield call(cancel, { id, version })

      if (res.success) {
        message.success('提交成功')
        yield put({
          type: 'query',
          payload: {}
        })
      }
    },

    * openFormModal({ payload }, { call, put, select }) {
      let distributorItem = { id: payload.distributorId, name: payload.distributorName }

      yield put({
        type: 'showFormModal',
        payload: payload
      })
      yield put({
        type: 'setDistributor',
        payload: distributorItem
      })

    },

    * openAuditModal({ payload }, { call, put, select }) {
      let distributorItem = { id: payload.distributorId, name: payload.distributorName }

      yield put({
        type: 'showAuditModal',
        payload: payload
      })
      yield put({
        type: 'setDistributor',
        payload: distributorItem
      })

    },

    * distributorQuery({ payload }, { call, put, select }) {
      yield put({
        type: 'asyncDistributorData',
        payload: {
          ...payload
        }
      })
      const pageOptions = yield select(({ balanceChangeApply }) => balanceChangeApply.distributorState.pageOptions)
      const queryParam = yield select(({ balanceChangeApply }) => balanceChangeApply.distributorState.queryParam)

      const queryPayload = {
        ...pageOptions,
        ...queryParam
      }

      const res = yield call(listPaymenters, queryPayload)

      yield put({
        type: 'distributorQuerySuccess',
        payload: res
      })
    },

    * openDistributorModal({ payload }, { call, put, select }) {
      yield put({
        type: 'distributorQuery',
        payload: {}
      })

      yield put({
        type: 'showDistributorModal',
        payload: {}
      })
    }
  },
  reducers: {
    showFormModal(state, { payload }) {
      state.currentItem = payload
      state.formModalVisible = true

      return {
        ...state
      }
    },

    showDistributorModal(state, { payload }) {
      state.distributorState.distributorModalVisible = true
      state.distributorState.currentItem = {}

      return {
        ...state
      }
    },

    showAuditModal(state, { payload }) {
      state.currentItem = payload
      state.auditModalVisible = true

      return {
        ...state
      }
    },

    showViewModal(state, { payload }) {
      state.currentItem = payload
      state.viewModalVisible = true

      return {
        ...state
      }
    },

    showSubmitModal(state, { payload }) {
      state.currentItem = payload
      state.submitModalVisible = true

      return {
        ...state
      }
    },

    changeBalanceChangingReason(state, { payload }) {
      state.currentItem.reason = payload.reason
      if (payload.reason === "OTHER" || payload.reason === "") {
        state.contractNumVisible = false;
      } else {
        state.contractNumVisible = true;
      }
      return {
        ...state
      }
    },

    /**
     * 隐藏所有modal
     * @param state
     * @param payload
     * @return {{}}
     */
    hideModal(state, { payload }) {
      state.formModalVisible = false
      state.auditModalVisible = false
      state.viewModalVisible = false
      state.submitModalVisible = false
      state.distributorState.distributorModalVisible = false

      return {
        ...state
      }
    },

    hideDistributorModal(state, { payload }) {
      state.distributorState.distributorModalVisible = false

      return {
        ...state
      }
    },

    distributorQuerySuccess(state, { payload }) {
      state.distributorState.items = payload.content
      state.distributorState.pagination = {
        ...state.distributorState.pagination,
        current: payload.number + 1,
        total: payload.totalElements
      }
      return {
        ...state,
      }
    },

    setDistributor(state, { payload }) {
      if (Array.isArray(payload)) {
        state.distributorState.currentItem = payload[0]
      } else {
        state.distributorState.currentItem = payload
      }

      state.currentItem.distributorId = state.distributorState.currentItem.id
      state.currentItem.distributorName = state.distributorState.currentItem.name

      return {
        ...state,
      }
    },

    changeSelectedTags(state, { payload }) {
      return {
        ...state,
        selectedTags: payload.selectedTags
      }
    },
    expandFilter(state, { payload }) {
      return {
        ...state,
        isExpand: true
      }
    },
    unExpandFilter(state, { payload }) {
      return {
        ...state,
        isExpand: false
      }
    },
    asyncDistributorData(state, { payload }) {
      if (payload.isTablePagination) {
        delete payload.isTablePagination;
        state.distributorState.queryParam = { ...state.distributorState.queryParam, ...payload }
        return {
          ...state,
        }
      }
      state.distributorState.queryParam = { ...payload }
      return {
        ...state,
      }
    },
  }
})
